package gui;

import dal.object.MapObject;
import gui.event.SwitchObject;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Vector;


@SuppressWarnings("serial")
public class BallDrawer extends JPanel {
	private int halfImageWidth;
	private int imageWidth;
	private int imageHeight75;
	private int imageHeight25;
	private Image img;
	private Graphics gr;
	private Vector<MapObject> vmo;
	private Dimension dimension;
	
	private double x, y; //TODO this will not be here
	
	public BallDrawer(Dimension dim/*MapStruct ms*/) {
		this.setOpaque(false);
		
		dimension = dim; // ms.getDimension();

		imageWidth = 38; //img.getWidth();
		halfImageWidth = 19; //imageWidth/2;
		imageHeight75 = 33; //(int) (img.getHeight() * 0.75);
		imageHeight25 = 11; //(int) (img.getHeight() * 0.25);
		
		this.addMouseListener(new SwitchObject(vmo));
		
		// Setting panel size
		this.setSize(new Dimension(dimension.width * imageWidth + ((dimension.height > 1) ? halfImageWidth : 0), dimension.height * imageHeight75 + imageHeight25)); 
	}
	
	public void moveBall(double x, double y){

		/*ballX += _x;
		ballY += _y;
		ball.setLocation((int)ballX,(int) ballY);*/
		this.x += x;
		this.y += y;
		repaint();
	}
	
	// Drawing the ball
	protected void paintComponent(Graphics g) {
		if (img == null) {
			img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
			gr = img.getGraphics();
		}
		
		BufferedImage ball = FileIO.f("pic/ball.png");
		
		gr.drawImage(ball, (int)x/*(38-ball.getWidth())/2*/, (int)y/*(44-ball.getHeight())/2*/, null);
		
		g.drawImage(img, 0, 0, null);
	}
}